﻿namespace KinonekoSoftware.Studio.ViewModels
{
    partial class AppViewModel
    {
        private bool _isQuikEditPanelOpen;

        internal void RefreshLoadState()
        {
            RaiseUpdated(nameof(IsLoaded));
            RaiseUpdated(nameof(CurrentRepository));
            RaiseUpdated(nameof(RepositoryPath));
        }


        public void Snapshot(Action<object> snapshotCallback)
        {
            if (Current is TabViewModelCore tCore)
            {
                tCore.Snapshot(snapshotCallback);
            }
            else if (Current is TabViewModelSub tSub)
            {
                tSub.Snapshot(snapshotCallback);
            }
        }



        public void GotoSetting()
        {
            GoTo<SettingViewModel>();
        }

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------

        #region CommandHandlers : Projects

        private void DoLocateRepositoryCommand()
        {
            #if WINDOWS
            
            Platform.OpenLink(RepositoryPath);
            
            #else
            
            #endif
        }

        private async void DoExportRepositoryWikiCommand()
        {
            await Platform.Developing();
        }

        private async void DoSwitchRepositoryCommand(RepositoryCache target)
        {
            var runtime = Ioc.Get<RepositoryRuntime>();

            // 
            // 关闭世界观
            await runtime.CloseAsync();

            //
            // 清空缓存
            Platform.Cache.Clear();

            Notifications.Clear();



            if (target is not null)
            {
                //
                //
                var r = await runtime.LoadAsync(target.Path, DataMode.Release);

                if (r.IsFinished)
                {

                    GoTo<RepositoryGalleryViewModel>();
                }
                else
                {
                    var ai           = Ioc.Get<ApplicationInformation>();
                    var repoFileName = Path.Combine(ai.SettingFolder, Platform.FileName_Setting_Repo);
                    Repositories.Remove(target);
                    LaunchContext.RepositorySetting.Repositories.Remove(target);
                    await JSON.DumpAsync(LaunchContext.RepositorySetting, repoFileName, false);
                }
            }
        }

        private async void DoSyncRepositoryCommand()
        {
            await Platform.Developing();
        }

        private async void DoDeleteRepositoryCommand(RepositoryCache target)
        {
            if (target is null)
            {
                return;
            }
            
            if (!await WarningOperation(
                                       TitleSR.DangerOperation,
                                       "您正在删除该世界观的打开记录，确定是否删除？此项操作不会删除源文件！"))
            {
                return;
            }

            var repo    = Ioc.Get<RepositorySetting>();
            var runtime = Ioc.Get<RepositoryRuntime>();

            if (target.Id == CurrentRepository.Id)
            {
                // 
                // 关闭世界观
                await runtime.CloseAsync();
            }

            Repositories.Remove(target);
            RepositoryHashSet.Remove(target.Path);

            //
            // 清除世界观的缓存
            repo.Repositories.RemoveAt(repo.Repositories.IndexOf(x => x.Path == target.Path));
            repo.LastRepository = repo.Repositories.FirstOrDefault()?.Path;

            //
            // 删除世界观文件
            // using (var session = await BeginSession(SR.Title_Deleting))
            // {
            //     Directory.Delete(current.Path);
            //     await session.Await();
            // }
        }

        #endregion

        #region CommandHandlers : Notifications

        private void DoRemoveAllNotificationCommand()
        {
            Notifications.Clear();
        }

        #endregion

        private void DoOpenQuickPanelCommand()
        {
            IsQuikEditPanelOpen = true;
        }

        private void DoGotoSettingCommand()
        {
            GotoSetting();
        }

        private void DoGotoBugReportCommand()
        {
#if WINDOWS

            Platform.OpenLink("App.Feedback", true);

#else
#endif
        }

        private void DoGotoTutorialCommand()
        {
#if WINDOWS

            Platform.OpenLink("App.Tutorial", true);

#else
#endif
        }


        /// <summary>
        /// 获取或设置 <see cref="IsQuikEditPanelOpen"/> 属性。
        /// </summary>
        public bool IsQuikEditPanelOpen
        {
            get => _isQuikEditPanelOpen;
            set => SetValue(ref _isQuikEditPanelOpen, value);
        }

        //-------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------

        /// <summary>
        /// 
        /// </summary>
        public ICommandEX GotoSettingCommand { get; }
        public ICommandEX GotoTutorialCommand          { get; }
        public ICommandEX GotoBugReportCommand         { get; }
        public ICommandEX RemoveAllNotificationCommand { get; }
        public ICommandEX OpenQuickPanelCommand        { get; }

        public ICommandEX LocateRepositoryCommand     { get; }
        public ICommandEX ExportRepositoryWikiCommand { get; }
        public ICommandEX SwitchRepositoryCommand     { get; }
        public ICommandEX SyncRepositoryCommand       { get; }
        public ICommandEX DeleteRepositoryCommand     { get; }

    }
}